package cz.muni.fi.pa165.library.web_layer.readerPages;

import cz.muni.fi.pa165.library.api.BookService;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.BorrowService;
import cz.muni.fi.pa165.library.api.BorrowTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.AuthenticatedSession;
import cz.muni.fi.pa165.library.web_layer.HomePage;
import cz.muni.fi.pa165.library.web_layer.Template;
import cz.muni.fi.pa165.library.web_layer.adminPages.Borrow.AddBorrow;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author dulinka
 */
public class ShowBorrowReader extends Template {
    private static final long serialVersionUID = 30L; 
    public ShowBorrowReader(final PageParameters parameters) {
	super(parameters);
        BorrowService borrowService = (BorrowService) ApplicationContextProvider.getApplicationContext().getBean("borrowService");
        RepeatingView repeating = new RepeatingView("repeating");
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        AuthenticatedSession session = ((AuthenticatedSession)Session.get());
        for (BorrowTO borrow : borrowService.findBorrowsByReader(session.getReader())) {
            for (BookTO book: borrow.getTitlesTO()){
                AbstractItem item = new AbstractItem(repeating.newChildId());
                item.add(new Label("title", book.getTitle()));      
                item.add(new Label("author", book.getAuthor()));
                item.add(new Label("borrowdate", dateFormat.format(borrow.getBorrowDate())));
                item.add(new Label("expirationdate", dateFormat.format(borrow.getExpirationDate())));
                repeating.add(item);
            }          
       }
       add(repeating);  
    }
}